<?php if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}
/**
 * Class account extends MY_Controller
 * @author zhangchangshun
 * 移动端用户接口类(评论功能)
 */
class comments extends API_Controller {

    public function __construct() {
        parent::__construct();
        if(ENVIRONMENT=='production'){
            $this->_auto();
        }
        ini_set('date.timezone','Asia/Shanghai'); // 'Asia/Shanghai'

        $this->load->model('circle_model','mcircle');
        $this->load->model('topic_model','mtopic');
        $this->load->model('account_model','maccount');
        $this->load->model('comments_model','mcomments');
        $this->load->model('comment_msg_model','mcommentmsg');
        $this->load->model('long_article_model', 'mlong_article');
        $this->load->model('upload_model','mupload');

    }

    /**
     * 发表评论
     */
    public function create_comments(){
        //获取当前用户信息
        $user_info = $this->userinfo;
        if(!isset($user_info) && empty($user_info)){
            $this->vdata['returnCode']   = '401';
            $this->vdata['returnInfo'] = '请求失败';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            $uid = $user_info['id'];
            $data = $this->input->post();
            if(!isset($data) && empty($data)){
                $this->vdata['returnCode']   = '250';
                $this->vdata['returnInfo'] = '缺少参数';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }else{
                $content = $this->input->post('content');
                if(!isset($content) || trim($content)==''){
                    $this->vdata['returnCode']   = '250';
                    $this->vdata['returnInfo'] = '请填写评论内容';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= array();
                }else{
                    $rid = $data['rid'];
                    $type = $data['type'];
                    $pid = isset($data['pid']) ? $data['pid'] : 0;
                    $new_info = array();
                    if($pid!=0){
                        $commentinfo = $this->mcomments->get_one(array('id'=>$pid));
                        if($commentinfo['pid']==0){
                            $create_data = array(
                                'uid'=>$uid,
                                'content'=>userTextEncode(strip_tags(trim($content))),
                                'rid'=>$rid,
                                'type'=>$type,
                                'pid'=>$pid,
                                'created'=>time()
                            );
                            $comment_id = $this->mcomments->create($create_data);
                        }else{
                            $create_data = array(
                                'uid'=>$uid,
                                'chat_id'=>$commentinfo['uid'],
                                'content'=>userTextEncode(strip_tags(trim($content))),
                                'rid'=>$rid,
                                'type'=>$type,
                                'pid'=>$commentinfo['pid'],
                                'created'=>time()
                            );
                            $comment_id = $this->mcomments->create($create_data);
                        }

                        //追加返回评论内容
                        $new_info = $this->mcomments->get_one(array('id'=>$comment_id));
                        $user_info11 = $this->maccount->get_one(array('id'=>$new_info['uid']));
                        $user_info12 = $this->maccount->get_one(array('id'=>$new_info['chat_id']));
                        $new_info['uname']=$user_info11['uname'];
                        if(!empty($user_info12)){
                            $new_info['cname']=$user_info12['uname'];
                        }else{
                            $new_info['cname']='';
                        }
                        if($new_info['uid']==$uid){
                            $new_info['is_can']=0;
                        }else{
                            $new_info['is_can']=1;
                        }
                    }else{
                        $create_data = array(
                            'uid'=>$uid,
                            'content'=>userTextEncode(strip_tags(trim($content))),
                            'rid'=>$rid,
                            'type'=>$type,
                            'pid'=>$pid,
                            'created'=>time()
                        );
                        $comment_id = $this->mcomments->create($create_data);
                    }
                    if($comment_id){
                        if($pid==0){
                            if($type==1){
                                //原圈子的详细信息
                                $circle_info = $this->mcircle->get_one(array('id'=>$rid));
                                if($uid!=$circle_info['uid']){
                                    $com_msg = array(
                                        'uid'=>$circle_info['uid'],
                                        'chat_id'=>$uid,
                                        'rid'=>$rid,
                                        'type'=>$type,
                                        'intro'=>userTextEncode(strip_tags(trim($content))),
                                        'com_url'=>'',
                                        'create_time'=>time()
                                    );
                                    $this->mcommentmsg->create($com_msg);
                                }
                                $this->mcircle->set($rid,array('comments','comments + 1'));
                            }elseif($type==2){
                                //原长文章的详细信息
                                $long_article = $this->mlong_article->get_one(array('id'=>$rid));
                                if($uid!=$long_article['uid']){
                                    $com_msg = array(
                                        'uid'=>$long_article['uid'],
                                        'chat_id'=>$uid,
                                        'rid'=>$rid,
                                        'type'=>$type,
                                        'intro'=>userTextEncode(strip_tags(trim($content))),
                                        'com_url'=>'',
                                        'create_time'=>time()
                                    );
                                    $this->mcommentmsg->create($com_msg);
                                }
                                $this->mlong_article->set($rid,array('comments','comments + 1'));
                                $this->db->query("update boc_circle set comments=comments+1 where t_id=$rid and rid=0");
                            }elseif($type==3){
                                //原话题
                                $this->mtopic->set($rid,array('comments','comments + 1'));
                                $this->mtopic->set($rid,array('hots','hots + 1'));
                            }
                        }else{
                            if($type==1){
                                $circle_info = $this->mcircle->get_one(array('id'=>$rid));
                                $commentinfo = $this->mcomments->get_one(array('id'=>$pid));

                                if($commentinfo['pid']==0){
                                    $uids = array(
                                        $circle_info['uid'],$commentinfo['uid']
                                    );
                                }else{
                                    $commentinfo2 = $this->mcomments->get_one(array('id'=>$commentinfo['pid']));
                                    $uids = array(
                                        $circle_info['uid'],$commentinfo['uid'],$commentinfo2['uid']
                                    );
                                }
                                $uids = array_unique($uids);
                                //原圈子的详细信息
                                foreach($uids as $vv){
                                    if($vv!=$uid){
                                        $com_msg = array(
                                            'uid'=>$vv,
                                            'chat_id'=>$uid,
                                            'rid'=>$rid,
                                            'type'=>$type,
                                            'intro'=>userTextEncode(strip_tags(trim($content))),
                                            'com_url'=>'',
                                            'create_time'=>time()
                                        );
                                        $this->mcommentmsg->create($com_msg);
                                    }
                                }
                                $this->mcircle->set($rid,array('comments','comments + 1'));
                            }elseif($type==2){
                                $long_article = $this->mlong_article->get_one(array('id'=>$rid));
                                $commentinfo = $this->mcomments->get_one(array('id'=>$pid));
                                if($commentinfo['pid']==0){
                                    $uids = array(
                                        $long_article['uid'],$commentinfo['uid']
                                    );
                                }else{
                                    $commentinfo2 = $this->mcomments->get_one(array('id'=>$commentinfo['pid']));
                                    $uids = array(
                                        $long_article['uid'],$commentinfo['uid'],$commentinfo2['uid']
                                    );
                                }
                                $uids = array_unique($uids);
                                //原长文章的详细信息
                                foreach($uids as $vv){
                                    if($vv!=$uid){
                                        $com_msg = array(
                                            'uid'=>$vv,
                                            'chat_id'=>$uid,
                                            'rid'=>$rid,
                                            'type'=>$type,
                                            'intro'=>userTextEncode(strip_tags(trim($content))),
                                            'com_url'=>'',
                                            'create_time'=>time()
                                        );
                                        $this->mcommentmsg->create($com_msg);
                                    }
                                }
                                $this->mlong_article->set($rid,array('comments','comments + 1'));
                                $this->db->query("update boc_circle set comments=comments+1 where t_id=$rid and rid=0");

                            }elseif($type==3){
                                //话题
                                $commentinfo = $this->mcomments->get_one(array('id'=>$pid));
                                if($commentinfo['pid']==0){
                                    $uids = array(
                                        $commentinfo['uid']
                                    );
                                }else{
                                    $commentinfo2 = $this->mcomments->get_one(array('id'=>$commentinfo['pid']));
                                    $uids = array(
                                        $commentinfo['uid'],$commentinfo2['uid']
                                    );
                                }
                                $uids = array_unique($uids);
                                foreach($uids as $vv){
                                    if($vv!=$uid){
                                        $com_msg = array(
                                            'uid'=>$vv,
                                            'chat_id'=>$uid,
                                            'rid'=>$rid,
                                            'type'=>$type,
                                            'intro'=>userTextEncode(strip_tags(trim($content))),
                                            'com_url'=>'',
                                            'create_time'=>time()
                                        );
                                        $this->mcommentmsg->create($com_msg);
                                    }
                                }
                                $this->mtopic->set($rid,array('comments','comments + 1'));
                                $this->mtopic->set($rid,array('hots','hots + 1'));


                            }
                        }
                        $this->vdata['returnCode']   = '200';
                        $this->vdata['returnInfo'] = '评论成功';
                        $this->vdata['secure']     = JSON_SECURE;
                        $this->vdata['content']= $new_info;
                    }else{
                        $this->vdata['returnCode']   = '401';
                        $this->vdata['returnInfo'] = '评论失败';
                        $this->vdata['secure']     = JSON_SECURE;
                        $this->vdata['content']= array();
                    }
                }
            }
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 删除评论
     */
    public function delete_comment(){
        //获取当前用户信息
        $user_info = $this->userinfo;
        $uid = $user_info['id'];
        $com_id = $this->input->post('com_id');
        $comment_info = $this->mcomments->get_one(array('id'=>$com_id));
        if(empty($comment_info)){
            $this->vdata['returnCode']   = '401';
            $this->vdata['returnInfo'] = '评论不存在';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else {
            if($comment_info['uid']==$uid){
                //删除评论成功修改相应的圈子、长文章、话题的评论数
                $source_id = $comment_info['rid'];
                if($comment_info['type']==1){
                    $this->db->query("update boc_circle set comments=comments-1 where id=$source_id");
                }elseif($comment_info['type']==2){
                    $this->mlong_article->set($source_id,array('comments','comments - 1'));
                }elseif($comment_info['type']==3){
                    $this->mtopic->set($source_id,array('comments','comments - 1'));
                }
                $this->db->query("delete from boc_comments where id=$com_id and uid=$uid");
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '删除成功';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }else{
                $this->vdata['returnCode']   = '401';
                $this->vdata['returnInfo'] = '没有权限';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 处理是否可以评论
     */

    public function can_comment(){
        //获取当前用户信息
        $user_info = $this->userinfo;
        $uid = $user_info['id'];
        $com_id = $this->input->post('com_id');
        $comment_info = $this->mcomments->get_one(array('id'=>$com_id));
        if(empty($comment_info)){
            $this->vdata['returnCode']   = '401';
            $this->vdata['returnInfo'] = '评论不存在';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else {
            if($comment_info['uid']==$uid){
                $this->vdata['returnCode']   = '401';
                $this->vdata['returnInfo'] = '不可对自己评论';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }else{
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '可以评论';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 新评论列表展示
     */
    public function new_comment_lists(){
        $start = $this->input->post('page');
        $uid = $this->userinfo['id'];
        $limit = 10;
        $start = $limit*($start-1);
        $commentmsgs = $this->mcommentmsg->get_list($limit,$start,array('create_time'=>'desc'),array('uid'=>$uid,'is_read'=>0),"*");
        if(empty($commentmsgs)){
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            foreach($commentmsgs as $kk=>$vv){
                $user_info = $this->maccount->get_one(array('id'=>$vv['chat_id']));
                $commentmsgs[$kk]['uname']=$user_info['uname'];
                $photo = $this->mupload->get_one(array('id'=>$user_info['photo']));
                if(!empty($photo)){
                    $commentmsgs[$kk]['headimg'] = UPLOAD_URL.$photo['url'];
                }else{
                    $commentmsgs[$kk]['headimg'] = '';
                }
                $commentmsgs[$kk]['cre_time'] = date('m-d H:i',$vv['create_time']);
                $commentmsgs[$kk]['intro'] = mb_substr(str_replace('&nbsp;','',strip_tags(userTextDecode($vv['intro']))),0,45,'utf-8').'...';

            }
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= $commentmsgs;
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 已读新评论处理
     */
    public function do_read(){
        $id = $this->input->post('id');
        $uid = $this->userinfo['id'];
        $comment_info = $this->mcommentmsg->get_one(array('id'=>$id));
        if(empty($comment_info)){
            $this->vdata['returnCode']   = '401';
            $this->vdata['returnInfo'] = '请求失败';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else {
            $rid = $comment_info['rid'];
            $type = $comment_info['type'];
            $this->db->query("update boc_comment_msg set is_read=1 where uid=$uid and rid=$rid and type=$type");
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '请求成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 评论列表中的刷新已读处理
     */

    public function to_read(){
        $uid = $this->userinfo['id'];
        $rid = $this->input->post('id');     //对应圈子或者文章
        $type = $this->input->post('type');   //类型   1圈子2长文章
        if(!$type || !$rid){
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '缺少参数';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            $new_comment = $this->mcommentmsg->get_all(array('uid'=>$uid,'rid'=>$rid,'type'=>$type,'is_read'=>0));
            if(empty($new_comment)){
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '请求成功';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }else{
                $this->mcommentmsg->update(array('is_read'=>1),array('uid'=>$uid,'rid'=>$rid,'type'=>$type,'is_read'=>0));
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '请求成功';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }
        }
        $this->_send_json($this->vdata);
    }

}
